package com.itany.cloud.controller;

import com.itany.cloud.entity.User;
import com.itany.cloud.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/28 9:43
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Api(tags = "用户管理系统相关的API接口")
@RestController
@RequestMapping("/users")
public class UserController {

    static Map<Integer,User> users = new HashMap<>();
    static Integer id = 1;

    @ApiOperation(value = "这是用于查询所有用户信息的接口",notes = "备注:该功能最终被XXX进行了二次维护")
    @GetMapping
    public ResponseResult findAll(){
        List<User> list = new ArrayList<>(users.values());
        return ResponseResult.success(list);
    }

    @ApiOperation("这是根据主键查询用户信息的接口")
    @ApiImplicitParam(name = "id",value = "用户的主键")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        User user = users.get(id);
        return ResponseResult.success(user);
    }

    @ApiOperation("这是添加指定用户信息的接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",required = true),
            @ApiImplicitParam(name = "password",value = "密码",dataType = "String",required = true),
            @ApiImplicitParam(name = "phone",value = "电话",dataType = "String",required = true),
            @ApiImplicitParam(name = "address",value = "地址",dataType = "String",required = true)
    })
    @PostMapping
    public ResponseResult addUser(User user){
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @ApiOperation("这是根据主键删除指定用户信息的接口")
    @ApiImplicitParam(name = "id",value = "用户的主键",dataType = "Integer",required = true)
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Integer id){
        users.remove(id);
        return ResponseResult.success();
    }

//    @ApiOperation("这是修改指定用户信息的接口")
    @ApiIgnore
    @PutMapping
    public ResponseResult modifyUser(User user){
//        users.put(user.getId(),user);
        User u = users.get(user.getId());
        u.setUsername(user.getUsername());
        u.setPassword(user.getPassword());
        u.setPhone(user.getPhone());
        u.setAddress(user.getAddress());
        return ResponseResult.success();
    }

}
